package com.kinon.norogamilk.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.kinon.norogamilk.R;
import com.kinon.norogamilk.adapter.GoodSortNumAdapter;
import com.kinon.norogamilk.adapter.RecyclRaodAdapter;
import com.kinon.norogamilk.bean.GoodNumBean;
import com.kinon.norogamilk.bean.RaodBean;
import com.kinon.norogamilk.bean.RepairBean;
import com.kinon.norogamilk.http.MilkApi;
import com.kinon.norogamilk.http.StrCallback;
import com.kinon.norogamilk.ui.base.BaseActivity;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

import io.reactivex.disposables.CompositeDisposable;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by lhqq on 2017-12-20.
 * 补货
 */

public class RepairActivity extends BaseActivity implements
        View.OnClickListener, RecyclRaodAdapter.OnMyItemClickListener {

    protected CompositeDisposable mCompositeDisposable;
    private Button btn_back;
    private Button btn_sumbit;
    private RecyclerView recycl_road;
    private RecyclerView recycl_goodSort;
    private RecyclerView recycl_goodNum;
    private TextView tv_repairGood;
    private Button btn_determine;
    private ArrayList<RepairBean> repList;
    private ArrayList<RaodBean> raodList;
    private RecyclRaodAdapter raodAdapter;
    private int raodIndex = 0;
    private int goodId;
    private int goodNum = 1;
    private String goodName;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_layout);
        initView();

    }

    private void initView() {
        btn_back = findViewById(R.id.btn_back);
        btn_sumbit = findViewById(R.id.btn_sumbit);
        recycl_road = findViewById(R.id.recycl_road);
        recycl_goodSort = findViewById(R.id.recycl_goodSort);
        recycl_goodNum = findViewById(R.id.recycl_goodNum);
        tv_repairGood = findViewById(R.id.tv_repairGood);
        btn_determine = findViewById(R.id.btn_determine);

        repList = new ArrayList<>();
        raodList = new ArrayList<>();

        setRecyManager();
        mCompositeDisposable = new CompositeDisposable();
        getRaodInfo();
        addListener();
    }

    private void setRecyManager() {
        raodAdapter = new RecyclRaodAdapter(raodList, RepairActivity.this);
        recycl_road.setAdapter(raodAdapter);
        GridLayoutManager gridManager = new
                GridLayoutManager(RepairActivity.this, 10);
        gridManager.setSmoothScrollbarEnabled(false);
        recycl_road.setLayoutManager(gridManager);
        setRecyGoodManager();
    }

    private void setRecyGoodManager() {
        GoodSortNumAdapter sortAdapter = new
                GoodSortNumAdapter(addLiset(1), RepairActivity.this);
        GoodSortNumAdapter numAdapter = new
                GoodSortNumAdapter(addLiset(2), RepairActivity.this);
        recycl_goodSort.setAdapter(sortAdapter);
        recycl_goodNum.setAdapter(numAdapter);
        GridLayoutManager gridManager1 = new
                GridLayoutManager(RepairActivity.this, 2);
        GridLayoutManager gridManager2 = new
                GridLayoutManager(RepairActivity.this, 3);
        gridManager1.setSmoothScrollbarEnabled(false);
        recycl_goodSort.setLayoutManager(gridManager1);
        recycl_goodNum.setLayoutManager(gridManager2);
        sortAdapter.setOnItemClickListener(new GoodSortNumAdapter.OnMyItemClickListener() {
            @Override
            public void onItemClick(View view, GoodNumBean bean) {
                goodId = bean.getGoodid();
                goodName = bean.getGoodName();
                tv_repairGood.setText(goodName + "\t" + goodNum + "瓶");

            }
        });
        numAdapter.setOnItemClickListener(new GoodSortNumAdapter.OnMyItemClickListener() {
            @Override
            public void onItemClick(View view, GoodNumBean bean) {
                goodNum = bean.getGoodid();
                tv_repairGood.setText(goodName + "\t" + goodNum + "瓶");

            }
        });
    }

    private void addListener() {
        btn_back.setOnClickListener(this);
        btn_sumbit.setOnClickListener(this);
        btn_determine.setOnClickListener(this);
        raodAdapter.setOnItemClickListener(this);
    }

    private String getRepairData() {
//        repList.add(new RepairBean(3,mMaxNum));
//        repList.add(new RepairBean(4,mMinNum));
        Gson gson = new Gson();
        String data = gson.toJson(repList);
        Log.e("lh", "getRepairData==" + data);
        return data;
    }

    private void getRaodInfo() {

        Call<String> call = MilkApi.getInstance().getRaodInfo(sn);
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                try {
                    Gson gson = new Gson();
                    JSONArray array = new JSONArray(data);
                    for (int i = 0; i < array.length(); i++) {
                        raodList.add(gson.fromJson(array.get(i).toString(), RaodBean.class));
                    }
//                    adapter.notifyDataSetChanged();
                    raodAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });
    }

    private void restock() {
        Call<String> call = MilkApi.getInstance()
                .machinePw(sn, getRepairData());
        call.enqueue(new StrCallback<String>() {
            @Override
            public void onSuccess(Response<String> response, String data) {
                mToast("补货成功");
                finish();
            }

            @Override
            public void onError(int code, String error) {
                mToast(error);
            }
        });

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back:

                finish();
                break;
            case R.id.btn_determine:
//                repList.add(new RepairBean(raodList.get(raodIndex).getGoods_id(),
//                        goodNum,goodId));
                mToast("加入货道成功");
                break;
            case R.id.btn_sumbit:
                if (raodIndex + 1 <= raodList.size()) {
                    raodAdapter.setLastPosition(raodIndex + 1);

                }

                restock();
                break;

        }
    }

    private ArrayList<GoodNumBean> addLiset(int type) {
        ArrayList<GoodNumBean> list = new ArrayList<>();
        if (type == 1) {
            list.add(new GoodNumBean(1, "350ml酸奶"));
            list.add(new GoodNumBean(2, "500ml酸奶"));
            list.add(new GoodNumBean(3, "350ml鲜奶"));
            list.add(new GoodNumBean(4, "500ml鲜奶"));
            goodName = list.get(0).getGoodName();
            goodId = list.get(0).getGoodid();
        } else {
            list.add(new GoodNumBean(1, "1瓶"));
            list.add(new GoodNumBean(2, "2瓶"));
            list.add(new GoodNumBean(3, "3瓶"));
            list.add(new GoodNumBean(4, "4瓶"));
            list.add(new GoodNumBean(5, "5瓶"));
            goodNum = list.get(0).getGoodid();
        }
        return list;

    }


    @Override
    public void onItemClick(View view, int position) {
        raodIndex = position;
//        Log.e(TAG, "position =="+position);
//        raodIndex=raodList.get(position).getGoods_id();
    }
}
